﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Forms;
using Microsoft.WindowsMobile.PocketOutlook;

namespace ShoppingList
{
    public partial class Share : Form
    {
        ChooseContactDialog contactChooser = new ChooseContactDialog();
        ShoppingList listToSend;

        public Share()
        {
            InitializeComponent();
            contactChooser.ChooseContactOnly = true;
            contactChooser.HideNew = true;
            SetButtonState();
        }

        public DialogResult ShowDialog(ShoppingList listToSend)
        {
            this.listToSend = listToSend;
            lblStatus.Text = "Choose recipients for " + listToSend.Name;
            return ShowDialog();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (contactChooser.ShowDialog() == DialogResult.OK)
            {
                Contact contact = contactChooser.SelectedContact;
                if (contact.MobileTelephoneNumber.Length > 0)
                {
                    lstRecipients.Items.Add(contactChooser.SelectedContact);
                }
                else
                {
                    lblStatus.Text = "No mobile number on contact";
                }
                SetButtonState();
            }

        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            lstRecipients.Items.RemoveAt(lstRecipients.SelectedIndex);
            SetButtonState();
        }

        private void lstRecipients_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetButtonState();
        }

        private void SetButtonState()
        {
            //set send button state
            if (lstRecipients.Items.Count > 0) btnSend.Enabled = true;
            else btnSend.Enabled = false;

            //set remove button state
            if (lstRecipients.SelectedIndex >= 0) btnRemove.Enabled = true;
            else btnRemove.Enabled = false;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                //build message
                StringBuilder body = new StringBuilder(160);
                body.Append(Main.MESSAGE_IDENTIFIER);
                foreach (ShoppingItem item in listToSend.Items.Values)
                {
                    body.Append("|");
                    body.Append(item.Name);
                }

                //send to each contact
                SmsMessage msg = new SmsMessage();
                foreach (Contact receiver in lstRecipients.Items)
                {
                    msg.To.Add(new Recipient(receiver.MobileTelephoneNumber));
                }
                msg.Body = body.ToString();
                msg.Send();
                lblStatus.Text = "List sent";
            }
            catch( Exception ex )
            {
                lblStatus.Text = ex.Message;
            }
            Cursor.Current = Cursors.Default;

        }
    }
}